// 岗位查询接口
const express = require('express')
const jobRouter = express.Router()
const Job = require('../model/jobModel') //引入数据模型
const {
    formatDate,
    replaceKey,
    chineseToWord
} = require('../utils')
const verify_token = require('../middleware/verifyToken')
// 获取所有活动
jobRouter.get('/', verify_token, async (req, res) => {
    let result = await Job.find()
    res.json({
        status: 1,
        data: result
    })
})
// 根据hd_id获取相应活动数据
jobRouter.get('/hd_id/:id', verify_token, async (req, res) => {
    let result = await Job.findById(req.params.id)
    res.json({
        status: 1,
        msg: '加载成功!',
        data: result
    })
})
// 根据hd_id获取相应职位表json数据
jobRouter.get('/getzwinfo/:hd_id', async (req, res) => {
    let result = await Job.findOne({
        'hd_id': req.params.hd_id
    })
    res.json({
        status: 1,
        msg: '加载成功!',
        zwinfo: result.hd_zw,
        zwth: result.hd_zw_thfiled,
        condition: result.hd_condition
    })
})
// 新增活动
jobRouter.post('/add', verify_token, async (req, res) => {
    let thkey = req.body.hd_zw_thkey.split(',').filter(item => item)
    let hdcond = req.body.hd_condition

    req.body.hd_zw_thfiled = Object.keys(req.body.hd_zw[0])
    req.body.hd_zw = replaceKey(req.body.hd_zw, thkey)
    req.body.hd_condition = chineseToWord(hdcond)

    let addResult = await new Job(req.body).save()
    res.json({
        status: 1,
        msg: '创建成功！',
        data: addResult
    })
})
// 查询活动
jobRouter.get('/search/:id', verify_token, async (req, res) => {
    let seatmp = await Job.findById(req.params.id)
    res.json({
        status: 1,
        msg: '加载成功!',
        data: seatmp.hd_Collect
    })
})
// 修改活动
jobRouter.put('/edit/:id', verify_token, async (req, res) => {
    req.body.hd_condition = chineseToWord(req.body.hd_condition)
    let thkey = req.body.hd_zw_thkey.split(',').filter(item => item)
    req.body.hd_zw_thfiled = Object.keys(req.body.hd_zw[0])
    req.body.hd_zw = replaceKey(req.body.hd_zw, thkey)
    let result = await Job.findByIdAndUpdate(req.params.id, req.body)
    if (result) {
        res.json({
            status: 1,
            msg: '修改成功'
        })
    }
})
// 用户手机号注册
jobRouter.post('/register/:hd_id', async (req, res) => {
    let formattime = formatDate(new Date())
    let regResult = await Job.updateOne({
        hd_id: req.body.hd_id
    }, {
        $push: {
            hd_Collect: {
                area: req.body.area,
                phone: req.body.phone,
                zctime: formattime
            }
        },
        $inc: {
            hd_cyrs: 1
        }
    })
    if (regResult.nModified == 1) {
        res.json({
            status: 1,
            msg: '注册成功'
        })
    } else {
        res.json({
            status: 2,
            msg: '注册失败'
        })
    }
})
// 手机号是否已经注册
jobRouter.post('/isPhoneExist/:hd_id', async (req, res) => {
    let existResult = await Job.find({
        hd_id: req.body.hd_id,
        "hd_Collect.phone": req.body.phone
    }, {
        "hd_Collect": {
            $elemMatch: {
                "phone": req.body.phone
            }
        }
    })
    if (existResult.length == 0) {
        return res.json({
            status: 2,
            msg: '手机号未注册'
        })
    }
    res.json({
        status: 1,
        msg: '手机号已注册',
        data: existResult
    })
})
// 删除某个手机号
jobRouter.get('/deletePhone', verify_token, async (req, res) => {
    let deleone = await Job.updateOne({
        _id: req.query.routeId
    }, {
        $pull: {
            hd_Collect: {
                phone: req.query.phone
            }
        },
        $inc: {
            hd_cyrs: -1
        }
    })
    if (deleone.nModified == 1) {
        res.json({
            status: 1,
            msg: '删除成功!'
        })
    } else {
        res.json({
            status: 2,
            msg: '删除失败!'
        })
    }
})
// 删除活动
jobRouter.get('/delete', verify_token, async (req, res) => {
    let deletehd = await Job.deleteOne({
        _id: req.query._id
    })
    if (deletehd.deletedCount == 1) {
        res.json({
            status: 1,
            msg: '删除成功!'
        })
    } else {
        res.json({
            status: 2,
            msg: '删除失败!'
        })
    }
})

// 通过参数查询
jobRouter.get('/query', async (req, res) => {

})



module.exports = jobRouter